using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using Sunlit.House.Floors.Dto;
using Abp.Extensions;
using Sunlit.House.Authorization;

namespace Sunlit.House.Floors
{
    public class VillageAppService: AsyncCrudAppService<Village,VillageDto,long,GetAllVillageInput,
        CreateVillageInput,UpdateVillageInput>,IVillageAppService
    {
        public VillageAppService(IRepository<Village,long> repository):base(repository)
        {
        }
        protected override IQueryable<Village> CreateFilteredQuery(GetAllVillageInput input)
        {
            return base.CreateFilteredQuery(input)
                .Include(v=>v.Region)
                .WhereIf(!input.Name.IsNullOrWhiteSpace(), v => v.Name.Contains(input.Name))
                .WhereIf(input.RegionId.HasValue, v => v.RegionId == input.RegionId);
        }
        public async Task<List<VillageDto>> GetAllListAsync()
        {
            var list = await Repository.GetAllListAsync();
            return ObjectMapper.Map<List<VillageDto>>(list);
        }
        public override async Task<VillageDto> GetAsync(EntityDto<long> input)
        {
            var entity = await Repository.GetAll()
                .Include(v => v.Region)
                .FirstOrDefaultAsync(v => v.Id == input.Id);

            return ObjectMapper.Map<VillageDto>(entity);
        }
    }
}
